<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="公司" prop="company.id" :rules="[  ]">
              <GridSelect
                title="选择公司"
                labelName = 'companyName'
                labelValue = 'id'
                :value = "inputForm.company.id"
                :limit="1"
                @getValue='getValues(arguments)'
                :columns="[
                {
                  prop: 'companyName',
                  label: '企业名称'
                }
                ]"
                :searchs="[
                {
                  prop: 'companyName',
                  label: '企业名称'
                }
                ]"
                dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
                entityBeanName="enterpriseInformation"
                queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="创建人" prop="founder.id"
                :rules="[  ]">
              <GridSelect
                :disabled="inputForm.company.id==''"
                title="选择创建人"
                labelName = 'userName'
                labelValue = 'id'
                :value = "inputForm.founder.id"
                :limit="1"
                @getValue='(value) => {inputForm.founder.id=value}'
                :columns="[
                {
                  prop: 'userName',
                  label: '用户名'
                }
                ]"
                :searchs="[
                {
                  prop: 'userName',
                  label: '用户名'
                }
                ]"
                :dataListUrl=dataListUrl
                entityBeanName="enterpriseUserInformation"
                queryEntityUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="通知标题" prop="noticetitle"
                :rules="[
                 ]">
              <el-input v-model="inputForm.noticetitle"></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="浏览次数" prop="number"
                :rules="[
                 ]">
              <el-input v-model="inputForm.number" ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="通知内容" prop="noticecontent"
                :rules="[
                 ]">
            <WangEditor ref="noticecontentEditor" v-model="inputForm.noticecontent"/>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        companyName: '',
        userName: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          company: {
            id: ''
          },
          founder: {
            id: ''
          },
          noticetitle: '',
          number: '',
          noticecontent: '',
          remarks: ''
        },
        dataListUrl:'',//查创建人接口
      }
    },
    components: {
      GridSelect,
      WangEditor
    },
    methods: {
      getValues(value){
        // console.log(value,'value')
        this.inputForm.company.id=value[0];
        this.dataListUrl='/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/list?enterpriseInformation='+this.inputForm.company.id
      },
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建通知公告`
        } else if (method === 'edit') {
          this.title = '通知公告'
        } else if (method === 'view') {
          this.title = '查看通知公告'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.noticecontentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/companynotice/companyNotice/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.companyNotice)
              this.$refs.noticecontentEditor.init(this.inputForm.noticecontent)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/companynotice/companyNotice/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>